﻿@inherits BasePage

<GridRow>
    <GridCol Span="24">
        <div class="known">
            <div class="known-desc">
                <strong>@Language["Known.Title"]</strong><br />
                - @Language["Known.Note1"]<br />
                - @Language["Known.Note2"]<br />
                - @Language["Known.Note3"]
            </div>
            <div class="known-url">
                @Language["Known.WebSite"]<a href="@Config.SiteUrl" target="_blank">@Language["WebSite"]</a>
                <a href="@Config.SiteUrl/doc" target="_blank">@Language["Document"]</a><br />
                @Language["Known.Source"]<a href="@Config.GiteeUrl" target="_blank">GITEE</a>
                <a href="@Config.GithubUrl" target="_blank">GITHUB</a><br />
                @Language["Known.QQNo"]<img src="https://img.shields.io/badge/QQ-865982686-blue" alt="865982686" /><br />
                <a href="@Config.GiteeUrl/stargazers" target="_blank"><img src="@Config.GiteeUrl/badge/star.svg?theme=dark" alt="star"></a>
                <a href="@Config.GithubUrl" target="_blank"><img src="https://img.shields.io/github/stars/known/known?color=%231890FF" alt="stars"></a>
            </div>
        </div>
    </GridCol>
</GridRow>
<GridRow>
    <GridCol Span="24">
        <div class="ws-space">
            <div class="ws-welcome">
                <img class="ws-avatar" src="@user?.AvatarUrl" />
                <div class="ws-info">
                    <span class="ws-name">@GetUserGreeting()</span>
                    <span class="ws-tips">@Language["Home.Today"].Replace("{date}", DateTime.Now.ToString("yyyy-MM-dd dddd"))</span>
                </div>
            </div>
            <ul class="count">
                <li>
                    <span class="name">@Language["Home.UserCount"]<span class="month">@Language["Home.Total"]</span></span>
                    <span class="amount">@info?.Statistics?.UserCount</span>
                </li>
                <li>
                    <span class="name">@Language["Home.LogCount"]<span class="month">@Language["Home.Total"]</span></span>
                    <span class="amount">@info?.Statistics?.LogCount</span>
                </li>
            </ul>
        </div>
    </GridCol>
</GridRow>
<GridRow>
    <GridCol Span="14">
        <div class="ws-chart">
            <Card Style="height:100%;">
                <CardTabs>
                    <Tabs DefaultActiveKey="1">
                        <TabPane Key="1">
                            <TabTemplate>@Language["Home.LogStatistic"]</TabTemplate>
                            <ChildContent>
                                <AntDesign.Charts.Column Config="chartConfig" Data="chartData" />
                            </ChildContent>
                        </TabPane>
                    </Tabs>
                </CardTabs>
            </Card>
        </div>
    </GridCol>
    <GridCol Span="10">
        <div class="ws-func">
            <Card Title="@Language["Home.CommonFunc"]" Style="height:100%;">
                @if (visitMenus != null && visitMenus.Count > 0)
                {
                    foreach (var item in visitMenus)
                    {
                        <div class="ws-func-menu" @onclick="e => Context.Navigate(item)">
                            <Icon Type="@item.Icon" Theme="outline" />
                            <span class="name">@Language.GetString(item)</span>
                        </div>
                    }
                }
            </Card>
        </div>
    </GridCol>
</GridRow>

@code {
    private UserInfo user;
    private HomeInfo info;
    private object[] chartData;
    private List<Known.MenuItem> visitMenus;
    private ColumnConfig chartConfig;

    protected override async Task OnInitPageAsync()
    {
        await base.OnInitPageAsync();

        user = CurrentUser;
        chartConfig = new()
        {
            Name = Language["Home.VisitTitle"].Replace("{month}", $"{DateTime.Now:yyyyMM}"),
            Height = 274,
            Padding = "auto",
            XField = "key",
            YField = "value",
            Meta = new
            {
                Key = new { Alias = Language["Home.Date"] },
                Value = new { Alias = Language["Home.Count"] }
            }
        };

        var service = new Services.HomeService(Context);
        info = await service.GetHomeAsync();
        chartData = info?.Statistics?.LogDatas?.FirstOrDefault()?.Series.Select(d => new { d.Key, d.Value }).ToArray();
        visitMenus = Context.GetMenus(info?.VisitMenuIds);
    }

    private string GetUserGreeting()
    {
        var hour = DateTime.Now.Hour;
        var greet = GetGreeting("Greeting0", user);
        if (5 <= hour && hour < 9) greet = GetGreeting("Greeting5", user);
        else if (9 <= hour && hour < 11) greet = GetGreeting("Greeting9", user);
        else if (11 <= hour && hour < 13) greet = GetGreeting("Greeting11", user);
        else if (13 <= hour && hour < 15) greet = GetGreeting("Greeting13", user);
        else if (15 <= hour && hour < 18) greet = GetGreeting("Greeting15", user);
        else if (18 <= hour && hour < 22) greet = GetGreeting("Greeting18", user);
        else if (22 <= hour && hour < 23) greet = GetGreeting("Greeting22", user);
        else if (23 <= hour || hour < 5) greet = GetGreeting("Greeting23", user);
        return greet;
    }

    private string GetGreeting(string id, UserInfo user) => Language[id].Replace("{name}", user?.Name);
}